<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgpu - materialx noise</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="example.css">
	</head>
	<body>

		<div id="info">
			<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>

			<div class="title-wrapper">
				<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>MaterialX Noise</span>
			</div>

			<small>MaterialX noise functions.</small>
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.webgpu.js",
					"three/webgpu": "../build/three.webgpu.js",
					"three/tsl": "../build/three.tsl.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three/webgpu';
			import { normalWorld, time, mx_noise_vec3, mx_worley_noise_vec3, mx_cell_noise_float, mx_fractal_noise_vec3 } from 'three/tsl';

			import { Inspector } from 'three/addons/inspector/Inspector.js';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
			import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js';

			let container;

			let camera, scene, renderer;

			let particleLight;
			let group;

			init();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.z = 100;

				scene = new THREE.Scene();

				group = new THREE.Group();
				scene.add( group );

				new HDRCubeTextureLoader()
					.setPath( 'textures/cube/pisaHDR/' )
					.load( [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ],
						function ( hdrTexture ) {

							const geometry = new THREE.SphereGeometry( 8, 64, 32 );

							const customUV = normalWorld.mul( 10 ).add( time );

							// left top

							let material = new THREE.MeshPhysicalNodeMaterial();
							material.colorNode = mx_noise_vec3( customUV );

							let mesh = new THREE.Mesh( geometry, material );
							mesh.position.x = - 10;
							mesh.position.y = 10;
							group.add( mesh );

							// right top

							material = new THREE.MeshPhysicalNodeMaterial();
							material.colorNode = mx_cell_noise_float( customUV );

							mesh = new THREE.Mesh( geometry, material );
							mesh.position.x = 10;
							mesh.position.y = 10;
							group.add( mesh );

							// left bottom

							material = new THREE.MeshPhysicalNodeMaterial();
							material.colorNode = mx_worley_noise_vec3( customUV );

							mesh = new THREE.Mesh( geometry, material );
							mesh.position.x = - 10;
							mesh.position.y = - 10;
							group.add( mesh );

							// right bottom

							material = new THREE.MeshPhysicalNodeMaterial();
							material.colorNode = mx_fractal_noise_vec3( customUV.mul( .2 ) );

							mesh = new THREE.Mesh( geometry, material );
							mesh.position.x = 10;
							mesh.position.y = - 10;
							group.add( mesh );

							//

							scene.background = hdrTexture;
							scene.environment = hdrTexture;

						}

					);

				// LIGHTS

				particleLight = new THREE.Mesh(
					new THREE.SphereGeometry( 0.4, 8, 8 ),
					new THREE.MeshBasicMaterial( { color: 0xffffff } )
				);
				scene.add( particleLight );

				particleLight.add( new THREE.PointLight( 0xffffff, 1000 ) );

				renderer = new THREE.WebGPURenderer( { antialias: true } );
				renderer.setAnimationLoop( render );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.inspector = new Inspector();
				container.appendChild( renderer.domElement );

				//

				renderer.toneMapping = THREE.ACESFilmicToneMapping;
				renderer.toneMappingExposure = 1.25;

				// EVENTS

				new OrbitControls( camera, renderer.domElement );

				window.addEventListener( 'resize', onWindowResize );

			}

			//

			function onWindowResize() {

				const width = window.innerWidth;
				const height = window.innerHeight;

				camera.aspect = width / height;
				camera.updateProjectionMatrix();

				renderer.setSize( width, height );

			}

			//

			function render() {

				const timer = Date.now() * 0.00025;

				particleLight.position.x = Math.sin( timer * 7 ) * 30;
				particleLight.position.y = Math.cos( timer * 5 ) * 40;
				particleLight.position.z = Math.cos( timer * 3 ) * 30;

				for ( let i = 0; i < group.children.length; i ++ ) {

					const child = group.children[ i ];
					child.rotation.y += 0.005;

				}

				renderer.render( scene, camera );

			}

		</script>
	</body>
</html>
